package com.smarth.zhizulian.app;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.utils.KumaToast;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by KUMA on 2016/3/9.
 */
public class BaseFragment extends Fragment {
    protected LayoutInflater inflater;
    private ViewGroup container;
    protected View contentView;
    protected Context mContext;
    private Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        this.container = container;
        mContext = getContext();
        onCreateView(savedInstanceState);
        if (contentView == null)
            return super.onCreateView(inflater, container, savedInstanceState);
        return contentView;
    }

    /**
     * 子类实现该方法 简化fragment添加View的方法 等同于fragment的onCreateView
     **/
    protected void onCreateView(Bundle savedInstanceState) {

    }

    /**
     * 模仿Activity查找控件
     **/
    public View findViewById(int id) {
        return contentView.findViewById(id);
    }


    public View getContentView() {
        return contentView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected void setTitlebar(String title) {
//        TitleBar titleBar = (TitleBar) findViewById(R.id.);
//        titleBar.setTitle(title);
        TextView titlebar_title = (TextView) findViewById(R.id.titlebar_title);
        titlebar_title.setText(title);
    }

    public void showMsg(String str) {
        KumaToast.show(mContext, str);
    }

    /**
     * 仿照Acitivity添加布局的方式
     **/
    public void setBKView(int layoutResID) {
        setBKView(inflater.inflate(layoutResID, container, false));
    }

    public void setBKView(View view) {
        contentView = view;
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        contentView = null;
        container = null;
        inflater = null;
        mContext = null;
    }

}
